﻿using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using MagnOS.Drivers;
using MagnOS.Command;
using NoobOS.FileSystem.Physical;
using NoobOS.FileSystem.Physical.Drivers;
using NoobOS;
using Cosmos.Hardware;

namespace MagnOS
{
    public class Kernel : Sys.Kernel
    {
        Shell TERM;
        protected override void BeforeRun()
        {
            IDE[] IDEDevices = new IDE[IDE.Devices.Count];
            VGAScreen VGA = new VGAScreen();
            VGA.SetTextMode(VGAScreen.TextSize.Size80x25);
            VGA.Clear(4);
            //Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.Gray;

            Console.WriteLine("Initializing ATA...\n\nScanning...\n");
            ATABus ATA01 = new ATABus(0, 1);
            Console.WriteLine("PORT " + ATA01.BusPort + " ST " + ATA01.ControllerPort + "\n\n");
            ATABus ATA11 = new ATABus(1, 1);
            Console.WriteLine("PORT " + ATA11.BusPort + " ST " + ATA11.ControllerPort + "\n\n");

            Console.WriteLine("Initializing IDE..\n\nScanning...\n");
            
            for (int x = 0; x < IDE.Devices.Count; x++)
            {
                Console.WriteLine("Device at " + x + ": " + IDE.Devices[x].BlockCount + "\n");
                IDEDevices[x] = IDE.Devices[x];
            }

                Console.WriteLine("Initializing ATAPI ...\n\nSearching for Hardware on bus 1...\n");

            AtapiHandler p0 = new AtapiHandler(1);
            Console.WriteLine(p0.ModelNo + " ");
            Console.WriteLine(p0.SerialNo + " ");
            Console.WriteLine(p0.FirmwareRev + " ");

            Console.WriteLine("\n\nAttempt to read in bus 1...\n");
            Byte[] data = new Byte[p0.BlockSize];
            bool s = true;
            try
            {
                p0.ReadBlock(0, 1, data);
            }catch{
            //Console.WriteLine(Utils.byteArrayToByteString(data));
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("FAILED TO READ ON BUS 1!\n");
                s = false;
            }
            if (s) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Success...\n");
            }
            Console.ForegroundColor = ConsoleColor.Gray;

            TERM = new Shell();
        }

        protected override void Run()
        {
            TERM.ProcessCommand(TERM.GetCommand());
        }
    }
}
